﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Controls.Primitives;

namespace ibba.src.sl.componentes
{
    [TemplateVisualState(Name="Normal", GroupName="ViewStates"),
     TemplateVisualState(Name="Flipped", GroupName="ViewStates"),
     TemplatePart(Name="FlipButton",Type=typeof(ToggleButton)),
     TemplatePart(Name="FlipButtonAlternative", Type=typeof(ToggleButton))]
    public class FlipPanel: Control
    { 
        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FrontContentProperty =
            DependencyProperty.Register("FrontContent", typeof(object), typeof(FlipPanel), null);

        public static readonly DependencyProperty BackContentProperty =
            DependencyProperty.Register("BackContent", typeof(object), typeof(FlipPanel), null);

        public static readonly DependencyProperty IsFlippedProperty =
            DependencyProperty.Register("IsFlipped", typeof(bool), typeof(FlipPanel), null);

        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(FlipPanel), null);


        public object FrontContent
        {
            get { return (object)GetValue(FrontContentProperty); }
            set { SetValue(FrontContentProperty, value); }
        }

        public object BackContent
        {
            get { return (object)GetValue(BackContentProperty); }
            set { SetValue(BackContentProperty, value); }
        }

        public bool IsFlipped
        {
            get { return (bool)GetValue(IsFlippedProperty); }
            set { SetValue(IsFlippedProperty, value); }
        }

        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        public FlipPanel()
        {
            DefaultStyleKey = typeof(FlipPanel);
        }
        
        private void ChangeVisualState(bool useTransition)
        {
            if (!IsFlipped)
                VisualStateManager.GoToState(this, "Normal", useTransition);
            else
                VisualStateManager.GoToState(this, "Flipped", useTransition);

        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            
            ToggleButton _flipButton = base.GetTemplateChild("FlipButton") as ToggleButton;

            if (_flipButton != null)
                _flipButton.Click +=new RoutedEventHandler(FlipButton_Click);

            
            ToggleButton _flipButtonAlternate = base.GetTemplateChild("FlipButtonAlternate") as ToggleButton;

            if (_flipButtonAlternate != null)
                _flipButtonAlternate.Click += new RoutedEventHandler(FlipButton_Click);
  
            
            ChangeVisualState(false);
        }
        
        void FlipButton_Click(object sender, RoutedEventArgs e)
        {
            this.IsFlipped = !this.IsFlipped;
            ChangeVisualState(true);
        }
    }
}